/*
 * Copyright (c) 2019 Nanjing Wind Information Co., Ltd. All Rights Reserved;
 * @Package wind.autotest.database
 * @version 1.0.0
 * @Date 2019年4月26日
 * @author jlliu.Farrior@wind.com.cn
 */
package wind.autotest.database.suport;

import java.sql.SQLException;

import javax.annotation.PostConstruct;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import wind.autotest.config.DataBaseProperties;
import wind.autotest.util.ExceptionUtils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;

/**
 * @Description: <类功能描述>
 * @author jlliu.Farrior@wind.com.cn
 * @Date 2019年4月26日 下午7:50:16
 * 
 */
@Slf4j
@Component
public class DbConnetManager {

	private DruidDataSource druidDataSource = null;
	
	@Autowired
	private DataBaseProperties dbproperties;
	
	public void init() {
		try {
			 // DruidDataSrouce工厂模式
			druidDataSource = (DruidDataSource) DruidDataSourceFactory
					.createDataSource(dbproperties.getDatasource());
		} catch (Exception e) {
			log.error("数据库连接初始化异常{}", ExceptionUtils.getFullStackTrace(e));
		}

	}

	public DruidDataSource getDataSource() {
		DruidDataSource druidDataSource = null;
		try {
			 // DruidDataSrouce工厂模式
			druidDataSource = (DruidDataSource) DruidDataSourceFactory
					.createDataSource(dbproperties.getDatasource());
		} catch (Exception e) {
			log.error("数据库连接初始化异常{}", ExceptionUtils.getFullStackTrace(e));
		}
		return druidDataSource;
	}
	
	
	public DruidPooledConnection getConnection(DatabaseType database) throws SQLException {
	
		if (null == druidDataSource) {
			init();
		}
		return druidDataSource.getConnection();
	}
}
